<template>
  <BasicContainer>
    <FyCurd
      ref="crud"
      right-search
      :page.sync="page"
      :data="tableData"
      :permission="permissionList"
      :table-loading="tableLoading"
      :option="tableOption"
      @on-load="getList"
      @search-change="searchChange"
      @refresh-change="refreshChange"
      @size-change="sizeChange"
      @current-change="currentChange"
      @row-del="handleDel">
      <template #menuLeft>
        <ElButton
          v-if="permissions.admin_sysuserinfo_add"
          class="filter-item"
          type="primary"
          size="small"
          icon="el-icon-edit"
          @click="addOrUpdateHandle(0)">
          添加
        </ElButton>
        <ElButton
          v-if="permissions.admin_sysuserinfo_export"
          class="filter-item"
          plain
          type="primary"
          size="small"
          icon="el-icon-download"
          @click="exportExcel">
          导出
        </ElButton>
        <ElButton class="filter-item" plain type="primary" size="small" icon="el-icon-download" @click="syncData">
          同步到oa
        </ElButton>
      </template>
      <template #type="scope">
        <ElTag v-if="scope.row.type === '退休'" type="warning">{{ scope.row.type }}</ElTag>
        <ElTag v-else>{{ scope.row.type }}</ElTag>
      </template>
      <template #menu="scope">
        <ElButton
          v-if="permissions.admin_sysuserinfo_edit"
          type="text"
          size="small"
          icon="el-icon-edit"
          @click="addOrUpdateHandle(scope.row.id)">
          编辑
        </ElButton>
        <ElButton
          v-if="permissions.admin_sysuserinfo_edit && scope.row.type !== '退休'"
          type="text"
          size="small"
          icon="el-icon-edit"
          @click="retireDialog(scope.row.id)">
          退休
        </ElButton>
      </template>
    </FyCurd>
    <!-- 弹窗, 新增 / 修改 -->
    <TableForm v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="refreshChange" />
  </BasicContainer>
</template>

<script>
  import { delObj, fetchList, putObj, retireUser, syncData } from '@/api/admin/userinfo'
  import { tableOption } from '@/const/crud/admin/userinfo'
  import { mapState } from 'vuex'
  import TableForm from './userinfoDialog'
  import { services } from '@/config'

  export default {
    name: 'FormalUserinfo',
    components: {
      TableForm
    },
    data() {
      return {
        searchForm: {},
        tableData: [],
        page: {
          total: 0, // 总页数
          currentPage: 1, // 当前页数
          pageSize: 20 // 每页显示多少条
        },
        tableLoading: false,
        tableOption,
        addOrUpdateVisible: false
      }
    },
    computed: {
      ...mapState('user', ['permissions']),
      permissionList() {
        return {
          addBtn: this.$isEmpty(this.permissions.admin_sysuserinfo_add, false),
          delBtn: this.$isEmpty(this.permissions.admin_sysuserinfo_del, false),
          editBtn: this.$isEmpty(this.permissions.admin_sysuserinfo_edit, false)
        }
      }
    },
    methods: {
      addOrUpdateHandle(id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      // 列表查询
      getList(page, params) {
        this.tableLoading = true
        this.searchForm.type = '正式'
        fetchList(
          Object.assign(
            {
              current: page.currentPage,
              size: page.pageSize
            },
            params,
            this.searchForm
          )
        )
          .then(response => {
            this.tableData = response.data.data.records
            this.page.total = response.data.data.total
            this.tableLoading = false
          })
          .catch(() => {
            this.tableLoading = false
          })
      },
      // 删除
      handleDel: function (row, index) {
        this.$confirm(`是否确认删除ID为${row.id}`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(() => {
            return delObj(row.id)
          })
          .then(data => {
            this.$message.success('删除成功')
            this.getList(this.page)
          })
      },
      // 更新
      handleUpdate: function (row, index, done, loading) {
        putObj(row)
          .then(data => {
            this.$message.success('修改成功')
            done()
            this.getList(this.page)
          })
          .catch(() => {
            loading()
          })
      },
      // 每页条数改变事件
      sizeChange(pageSize) {
        this.page.pageSize = pageSize
      },
      // 当前页发生改变事件
      currentChange(current) {
        this.page.currentPage = current
      },
      // 查询事件
      searchChange(form, done) {
        this.searchForm = form
        this.page.currentPage = 1
        this.getList(this.page, form)
        done()
      },
      // 刷新事件
      refreshChange() {
        this.getList(this.page)
      },
      // 导出excel
      exportExcel() {
        this.$downBlobFile(
          `${services.adminService}sysuserinfo/export`,
          this.searchForm,
          `在职人员信息${this.$dateFormat(new Date())}.xlsx`,
          'post'
        )
      },
      syncData() {
        syncData().then(rps => {
          this.$message.success(rps.data.data)
        })
      },
      retireDialog(id) {
        this.$confirm('是否确认为退休人员,修改之后将不能登录本系统', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(() => {
            retireUser(id)
          })
          .then(data => {
            this.$message.success('修改成功')
            this.getList(this.page)
          })
      }
    }
  }
</script>
